package net.zhengxinyun.performance.controller.login;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.EvaluateUserEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.filter.EvaluateUserFilter;
import net.zhengxinyun.performance.service.login.EvaluateUserService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/17.
 */
@RestController
@RequestMapping("/evaluateUser")
public class EvaluateUserController {

    private EvaluateUserService evaluateUserService;
    private TokenService tokenService;
    private LoginService loginService;

    @Autowired
    public EvaluateUserController(EvaluateUserService evaluateUserService, TokenService tokenService, LoginService loginService) {
        this.evaluateUserService = evaluateUserService;
        this.tokenService = tokenService;
        this.loginService = loginService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = evaluateUserService.insert(param.evaluateUserEntity);
            if (!"添加成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = evaluateUserService.update(param.evaluateUserEntity);
            if (!"修改成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = evaluateUserService.delete(param.list);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody EvaluateUserParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            List<EvaluateUserFilter> list = evaluateUserService.queryAll(param.evaluateUserFilter);
            if (list.size() == 0){
                param.evaluateUserFilter.setPageNumber(param.evaluateUserFilter.getPageNumber()-1);
                list = evaluateUserService.query(param.evaluateUserFilter);
            }
            Double pageTotal = Util.intGetTotalPage(evaluateUserService.queryForCount(param.evaluateUserFilter),param.evaluateUserFilter.getPageSize());
            HashMap<String,Object> map = new HashMap<>();
            map.put("list",list);
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对",null);
    }

    @PostMapping("/importEvaluateUser")
    public Result<HashMap<String,Object>> importEvaluateUser(@RequestParam(value = "file")MultipartFile file){
        HashMap<String, Object> map = evaluateUserService.importEvaluateUser(file);

        if (!"200".equals(map.get("code").toString())){


            return ResultUtils.getResults(Result.Status.ERROR,"导入失败",map);
        }
        return ResultUtils.getSuccessResults(map);
    }


    /**
     * 批量修改用户分类， 县领导， 党代表，人大代表等
     * @param param
     * @return
     */
    @PostMapping("/batchUpdate")
    public Result<String> batchUpdate(@RequestBody EvaluateUserParam param)
    {
        if (tokenService.compareToken(param.tokenEntity.getValue())){

            List<Long> userIds = param.evaluateUserEntity.getUserIds();
            String userType = param.evaluateUserEntity.getUserType();
            if(ObjectUtils.isBlank(userIds)||ObjectUtils.isBlank(userType))
            {
                return ResultUtils.getResults(Result.Status.ERROR,"用户id为空或者分类为空，请勾选需要修改的用户或选择用户分类后重试^_^");
            }

            for (Long userId : userIds) {

                EvaluateUserEntity entity = evaluateUserService.selectById(userId);
                entity.setUserType(userType);
                evaluateUserService.update(entity);

            }

        }


        return ResultUtils.getSuccessResults();
    }

}
class EvaluateUserParam{
    public EvaluateUserEntity evaluateUserEntity;
    public EvaluateUserFilter evaluateUserFilter;
    public TokenEntity tokenEntity;
    public List<String> list;
}
